<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_vote;
use cmf\lib\Plugin;
use think\db;

class QbtVotePlugin extends Plugin
{

    public $info = [
        'name'        => 'QbtVote',
        'title'       => '投票管理',
        'description' => '投票管理',
        'status'      => 1,
        'author'      => '千佰特',
        'version'     => '1.0',
        'demo_url'    => 'http://www.qbt8.com',
        'author_url'  => 'http://www.qbt8.com',
    ];


    public $hasAdmin = 1;//插件是否有后台管理界面

    // 插件安装
    public function install()
    {
        //投票表
        Db::query("DROP TABLE IF EXISTS `qbtcmf_vote`");
        Db::query("
CREATE TABLE `qbtcmf_vote` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL COMMENT '名称',
  `everytime` int(11) NOT NULL COMMENT '每次投几票',
  `start_time` int(10) NOT NULL COMMENT '开始时间',
  `end_time` int(10) NOT NULL COMMENT '结束时间',
  `city` text COMMENT '投票区域',
  `state` tinyint(4) NOT NULL COMMENT '状态',
  `create_time` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4;

            
            ");

        //投票项子表
        Db::query("DROP TABLE IF EXISTS `qbtcmf_vote_item`");
        Db::query("
CREATE TABLE `qbtcmf_vote_item` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `vote_id` int(11) NOT NULL COMMENT '父id',
  `name` varchar(255) NOT NULL COMMENT '文字',
  `url` varchar(255) NOT NULL,
  `image` varchar(255) NOT NULL COMMENT '图片',
  `count` int(11) NOT NULL COMMENT '票数',
  `status` tinyint(4) NOT NULL COMMENT '0显示 1隐藏',
  `list_order` int(10) NOT NULL DEFAULT '10',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;

            
            ");
        
        //投票记录
        Db::query("DROP TABLE IF EXISTS `qbtcmf_vote_record`");
        Db::query("
            CREATE TABLE `qbtcmf_vote_record` (
              `id` int(11) NOT NULL,
              `vote_id` int(11) NOT NULL,
              `vote_item_id` int(11) NOT NULL,
              `identify` varchar(255) NOT NULL COMMENT 'cookie标识',
              `ip` varchar(255) NOT NULL,
              `create_time` int(10) DEFAULT NULL COMMENT '投票时间',
              PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

            
            ");
        
        //投票记录
        Db::query("DROP TABLE IF EXISTS `qbtcmf_vote_log`");
        Db::query("
CREATE TABLE `qbtcmf_vote_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `vote_id` int(11) NOT NULL,
  `item_id` int(11) NOT NULL,
  `ip` varchar(255) NOT NULL,
  `create_time` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4;
        
            ");

        $data['parent_id'] = 0;
        $data['type'] = 0;
        $data['status'] = 1;
        $data['icon'] = 'bar-chart';
        $data['app'] = 'plugin/QbtVote';
        $data['controller'] = 'AdminIndex';
        $data['action'] = 'index';
        $data['name'] = '投票管理';
        $data['remark'] = '投票管理';
        $id = Db::table('qbtcmf_admin_menu')->insertGetId($data);
        $data1['parent_id'] = $id;
        $data1['type'] = 0;
        $data1['status'] = 1;
        $data1['app'] = 'plugin/QbtVote';
        $data1['controller'] = 'AdminIndex';
        $data1['action'] = 'index';
        $data1['name'] = '投票管理';
        $data1['remark'] = '投票管理';
        Db::table('qbtcmf_admin_menu')->insert($data1);

        return true;//安装成功返回true，失败false
    }

    // 插件卸载
    public function uninstall()
    {
        Db::query("DROP TABLE `qbtcmf_vote`");
        Db::query("DROP TABLE `qbtcmf_vote_item`");
        Db::query("DROP TABLE `qbtcmf_vote_log`");
        Db::query("DROP TABLE `qbtcmf_vote_record`");
        Db::table('qbtcmf_admin_menu')->where("app = 'plugin/QbtVote'")->delete();
        return true;//卸载成功返回true，.
    }
}